package com.goodboys.controller;

import com.goodboys.base.controller.BaseController;
import com.goodboys.base.entity.BaseReqBean;
import com.goodboys.base.resp.Response;
import com.goodboys.entity.UserTag;
import com.goodboys.service.TagService;
import com.goodboys.utils.JwtUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by CJ on 2018-05-27.
 */
@RestController
@RequestMapping("/tag")
public class TagController  extends BaseController{

    private final Logger logger = LoggerFactory.getLogger(TagController.class);

    @Autowired
    private TagService tagService;

    @PostMapping("/addUserTag")
    @ResponseBody
    public Response addUserTag(BaseReqBean baseReqBean){
        Response response = new Response();
        try {
            Map result = tagService.addUserTag(baseReqBean);
            return getRetunResult(result,response);
        } catch (Exception e) {
            logger.error("创建用户自定义标签失败。。。");
            e.printStackTrace();
            return response.failure("创建用户自定义标签失败，请重试。。。");
        }
    }

    @GetMapping("/isExistUserTag")
    @ResponseBody
    public Response isExistUserTag(BaseReqBean baseReqBean){
        Response response = new Response();
        try {
            Map result = tagService.isExistUserTag(baseReqBean);
            return getRetunResult(result,response);
        } catch (Exception e) {
            logger.error("判断此标签是否已经重复失败。。。");
            e.printStackTrace();
            return response.failure("判断此标签是否已经重复失败，请重试。。。");
        }
    }

    @PostMapping("/updateUserTag")
    @ResponseBody
    public Response updateUserTag(UserTag userTag,HttpServletRequest request){
        Response response = new Response();
        if(StringUtils.isEmpty(userTag.getUserId())) return response.failure("用户id为空，请重试。。。");
        if(!JwtUtils.isPermission(userTag.getUserId(),request)) return  response.failure("他人自定义标签不可更新。。");
        try {
            Map result = tagService.updateUserTag(userTag);
            return getRetunResult(result,response);
        } catch (Exception e) {
            logger.error("更新用户自定义标签失败。。。");
            e.printStackTrace();
            return response.failure("更新用户自定义标签失败，请重试。。。");
        }
    }


    @PostMapping("/deleteUserTag")
    @ResponseBody
    public Response deleteUserTag(UserTag userTag, HttpServletRequest request){
        Response response = new Response();
        if(StringUtils.isEmpty(userTag.getUserId())) return response.failure("用户id为空，请重试。。。");
        if(!JwtUtils.isPermission(userTag.getUserId(),request)) return  response.failure("他人自定义标签不可删除。。。");
        try {
            Map result = tagService.deleteUserTag(userTag);
            return getRetunResult(result,response);
        } catch (Exception e) {
            logger.error("删除用户自定义标签失败。。。");
            e.printStackTrace();
            return response.failure("删除用户自定义标签失败，请重试。。。");
        }
    }

    @GetMapping("/findTags")
    @ResponseBody
    public Response findTags(BaseReqBean baseReqBean){
        Response response = new Response();
        try {
            Map result = tagService.findTags(baseReqBean);
            return ((Integer)result.get("attribute") == 0) ? getRetunResult(result,response) : getRetunPageList(result,response);
        } catch (Exception e) {
            logger.error("查询用户/信息标签失败。。。");
            e.printStackTrace();
            return response.failure("查询用户/信息标签失败，请重试。。。");
        }
    }
}
